package com.hxgis.oc.util.idw;

import com.hxgis.oc.config.OConfig;
import com.hxgis.oc.util.CommandInvoker;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;

@Slf4j
public class GdalTools {
    public static void cutGrid(File inputFile, String cutLine, File outputFile, float nodataValue) {
        String app = OConfig.getConfig().getGdalPath() + "gdalwarp.exe";

        String[] command = new String[]{
                "-crop_to_cutline",
                "-cutline",
                cutLine,
                inputFile.getAbsolutePath(),
                outputFile.getAbsolutePath(),
                "-srcnodata", String.valueOf(nodataValue),
                "-dstnodata", String.valueOf(nodataValue),

        };

        try {
            CommandInvoker.invoke(app, command);
        } catch (IOException | InterruptedException e) {
            log.error(e.getMessage());
        }

    }

   public static void idwGrid(File inputFile, String zField, int width, int height,
                              File outputFile,
                              float increaseValue,
                              float multiply_value, float nodataValue){
        String app = OConfig.getConfig().getGdalPath() + "gdal_grid.exe";

        String[] command = new String[]{
            "-zfield", String.format("\"%s\"",zField), "-z_increase",String.valueOf(increaseValue),"-z_multiply",String.valueOf(multiply_value),
            inputFile.getAbsolutePath(),
            outputFile.getAbsolutePath(),
            "-outsize", String.valueOf(width), String.valueOf(height),
            "-ot", "UInt16",
            "-a","invdist:power=2.0:smoothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=" + nodataValue
        };

        try {
            CommandInvoker.invoke(app, command);
        } catch (IOException | InterruptedException e) {
            log.error(e.getMessage());
        }

   }

    public static void contour(File regionTiff, File contourShpFile, int f, String field) {
        String app = OConfig.getConfig().getGdalPath() + "gdal_contour.exe";

        String[] command = new String[]{
                regionTiff.getAbsolutePath(),
                contourShpFile.getAbsolutePath(),
                "-i",
                String.valueOf(f),
                "-a",
                field
        };

        try {
            CommandInvoker.invoke(app, command);
        } catch (IOException | InterruptedException e) {
            log.error(e.getMessage());
        }

    }

    public static void tiff2png(File tiffFile, File pngFile){
        String app = OConfig.getConfig().getGdalPath() + "gdal_translate.exe";

        String[] command = new String[]{
                tiffFile.getAbsolutePath(),
                pngFile.getAbsolutePath(),
        };

        try {
            CommandInvoker.invoke(app, command);
        } catch (IOException | InterruptedException e) {
            log.error(e.getMessage());
        }

    }

    public static boolean shp2geojson(String shpFileName,String geojsonFileName){
        String app = OConfig.getConfig().getGdalPath() + "ogr2ogr.exe";

        String[] command = new String[]{
                "-f",
                "geojson",
                geojsonFileName,
                shpFileName,
        };

        try {
            return CommandInvoker.invoke(app,command);
        } catch (IOException | InterruptedException e) {
            log.error(e.getMessage());
        }

        return false;
    }
}
